package HomeWork.Question4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

import static java.lang.System.exit;

public class Client {
    private final int send_port;
    DatagramSocket datagramSocket;
    BufferedReader bufferedReader = null;
    DatagramPacket datagramPacket = null;
    String data = null;
    Client(int send_port,DatagramSocket datagramSocket){
        this.send_port = send_port;
        this.datagramSocket = datagramSocket;
    }
    protected void send() throws IOException {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        byte[] datas;
        while (true){
            //准备数据  控制台读入
            data = bufferedReader.readLine();
            datas = data.getBytes(StandardCharsets.UTF_8);
            datagramPacket = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress("localhost", send_port));
            datagramSocket.send(datagramPacket);
            if(data.equals("bye")){
                exit (0);
            }
        }
    }
}
